package a16_ArrayCaculateValue;

import java.util.Random;

/**
 * @Author quan
 * @Description 随机打乱数组中元素的训练
 * @Date 2023/2/1 18:23
 */
public class randomizeArrayOrder {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5};
        //获取随机索引，random生成的数字个数就是arr的长度arr.length
        Random r = new Random();
        int randomIndex = r.nextInt(arr.length);
        //生成的随机数在0-4之间，因为arr的索引值是0-4
        for (int i = 0; i < arr.length; i++) {
            int temp = arr[i];
            arr[i] = arr[randomIndex];
            arr[randomIndex] = temp;
        }

        //遍历数组
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}
